/**
 * 
 */
package org.vsg.stock.basic.service.business;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.vsg.stock.core.domain.StockDailyKLine;
import org.vsg.stock.core.domain.StockDailyKLinePK;

/**
 * @author Bill Vison
 *
 */
class GoupingPointRefStockDailyKLine extends StockDailyKLine{
	
	private StockDailyKLine kline;
	
	private short refType;
	
	GoupingPointRefStockDailyKLine(StockDailyKLine kline , short refType) {
		this.kline = kline;
		this.refType = refType;
	}

	GoupingPointRefStockDailyKLine(StockDailyKLine kline , double refPrice) {
		this.kline = kline;
		
		double[] diffTo = new double[4];
		diffTo[0] = Math.abs( refPrice - kline.getHigh() );
		diffTo[1] = Math.abs( refPrice - kline.getLow()  );
		diffTo[2] = Math.abs( refPrice - kline.getOpen()  );
		diffTo[3] = Math.abs( refPrice - kline.getClose()  );
		
		double tmpDiff = 9999999.0;
		for (int i = 0 ; i < diffTo.length ; i++) {
			tmpDiff = tmpDiff > diffTo[i] ? diffTo[i] : tmpDiff;
		}
		
		if (tmpDiff == diffTo[0]) {
			this.refType = StockDailyKLine.HIGH;			
		} else if (tmpDiff == diffTo[1]) {
			this.refType = StockDailyKLine.LOW;			
		} else if (tmpDiff == diffTo[2]) {
			this.refType = StockDailyKLine.OPEN;			
		} else if (tmpDiff == diffTo[3]) {
			this.refType = StockDailyKLine.CLOSE;			
		}

		
	}
	
	short getRefType() {
		return this.refType;
	}
	
	
	public double getReferPrice() {
		switch (this.refType) {
			case StockDailyKLine.CLOSE :
				return this.kline.getClose();
			case StockDailyKLine.HIGH :
				return this.kline.getHigh();
			case StockDailyKLine.LOW :
				return this.kline.getLow();
			case StockDailyKLine.OPEN :
				return this.kline.getOpen();
			default :
				return -1;
		}
	}

	public Date getDate() {
		return kline.getDate();
	}

	public StockDailyKLinePK getId() {
		return kline.getId();
	}

	public double getOpen() {
		return kline.getOpen();
	}

	public double getHigh() {
		return kline.getHigh();
	}

	public double getLow() {
		return kline.getLow();
	}

	public double getClose() {
		return kline.getClose();
	}

	public double getYieldRate() {
		return kline.getYieldRate();
	}

	public long getVol() {
		return kline.getVol();
	}

	public double getTransactionValue() {
		return kline.getTransactionValue();
	}

	public short getExRights() {
		return kline.getExRights();
	}

	public double getMiddle() {
		return kline.getMiddle();
	}

	public double getAverage() {
		return kline.getAverage();
	}

	@Override
	public String toString() {
		return "GoupingPointRefStockDailyKLine [refType=" + refType
				+ ", getId()=" + getId() + ", refPrice="+ this.getReferPrice()+"]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((kline == null) ? 0 : kline.hashCode());
		result = prime * result + refType;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		GoupingPointRefStockDailyKLine other = (GoupingPointRefStockDailyKLine) obj;
		if (kline == null) {
			if (other.kline != null)
				return false;
		} else if (!kline.equals(other.kline))
			return false;
		if (refType != other.refType)
			return false;
		return true;
	}
	
	

}
